package router

import (
	"context"

	"gitee.com/linxing_3/sr-core/server/net/http/session"
	"gitee.com/mkwchecking/sr-admin/apps/devtools/adapter/http/api"
	"gitee.com/mkwchecking/sr-admin/apps/devtools/adapter/wire"
	"gitee.com/mkwchecking/sr-admin/pkg/http/middleware"
	"gitee.com/mkwchecking/sr-admin/pkg/plugins/phttp"
	"github.com/gin-gonic/gin"
)

type RouterRegister func(router *gin.RouterGroup, session *session.SessionManager, api *api.DevtoolsApi) error

var subRouters = []RouterRegister{}

func addRouter(router RouterRegister) {
	subRouters = append(subRouters, router)
}

func InitHttpRouter(ctx context.Context) error {

	api, err := wire.NewDevtoolsApi(ctx)
	if err != nil {
		return err
	}

	httpSvr := phttp.MustGetHttp()
	session, err := phttp.GetSessionE()
	if err != nil {
		return err
	}

	route := httpSvr.Router("/api/v1/devtools", middleware.Cors)

	for _, router := range subRouters {
		if err := router(route, session, api); err != nil {
			return err
		}
	}
	return nil
}
